/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles.android;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;

import com.googlecode.maurersmarbles.ScreenCanvas;

/**
 * Created by Daniel Uppenkamp on 1/17/2015.
 */
public class AndroidScreenCanvas extends ScreenCanvas {

    /**
     * The canvas that is used to draw the drawables.
     */
    protected Canvas canvas;
    /**
     * The paint that is used to draw the drawables.
     */
    protected Paint paint =  new Paint();

    /**
     * Creates a new AndroidScreenCanvas.
     */
    public AndroidScreenCanvas() {
        // do nothing
    }

    /**
     * Sets the canvas that is used to draw the drawables.
     * @param canvas the canvas that is used to draw the drawables
     */
    public void setCanvas(Canvas canvas) {
        this.canvas = canvas;
    }

    @Override
    public void drawLine(int x1, int y1, int x2, int y2, int rgb) {
        if(canvas != null) {
            paint.setColor(rgb);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawLine(x1, y1, x2, y2, paint);
        }
    }

    @Override
    public void drawOval(int x, int y, int width, int height, int rgb) {
        if(canvas != null) {
            paint.setColor(rgb);
            paint.setStyle(Paint.Style.STROKE);
            RectF rect = new RectF(x, y, x + width, y + height);
            canvas.drawOval(rect, paint);
        }
    }

    @Override
    public void drawRect(int x, int y, int width, int height, int rgb) {
        if(canvas != null) {
            paint.setColor(rgb);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(x, y, x + width, y + height, paint);
        }
    }

    @Override
    public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight, int rgb) {
        if(canvas != null) {
            paint.setColor(rgb);
            paint.setStyle(Paint.Style.STROKE);
            RectF rect = new RectF(x, y, x + width, y + height);
            canvas.drawRoundRect(rect, arcWidth, arcHeight, paint);
        }
    }

    @Override
    public void drawStringCentered(String text, int x, int y, int width, int height, int rgb) {
        if(canvas != null) {
            paint.setColor(rgb);
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            // TODO Make the font a constant
            paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
            paint.setTextAlign(Paint.Align.CENTER);
            // TODO Make the font size a constant variable somewhere
            paint.setTextSize(Math.min(width, height) / 2);
            int startX = (int) (x + (width / 2.0));
            int startY = (int) (y + ((height / 2.0) - ((paint.descent() + paint.ascent()) / 2.0)));
            canvas.drawText(text, startX, startY, paint);
        }
    }

    @Override
    public void fillOval(int x, int y, int width, int height, int rgb) {
        if(canvas != null) {
            paint.setColor(rgb);
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            RectF rect = new RectF(x, y, x + width, y + height);
            canvas.drawOval(rect, paint);
        }
    }

    @Override
    public void fillRect(int x, int y, int width, int height, int rgb) {
        if(canvas != null) {
            paint.setColor(rgb);
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            canvas.drawRect(x, y, x + width, y + height, paint);
        }
    }

    @Override
    public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight, int rgb) {
        if(canvas != null) {
            paint.setColor(rgb);
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            RectF rect = new RectF(x, y, x + width, y + height);
            canvas.drawRoundRect(rect, arcWidth, arcHeight, paint);
        }
    }
}
